package com.medium.house.controller;

import com.medium.house.entity.WechatUser;
import com.medium.house.service.WechatAuthService;
import com.medium.house.service.WechatUserService;
import com.medium.house.vo.ResultVO;
import com.medium.house.vo.ResultVOUtil;
import com.medium.house.vo.WechatSignVO;
import com.medium.house.request.WechatUserRequestDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Author : wang.yu
 * @Description :
 * @Date: Created in 2018/1/29 0029  17:25
 * @Modified By
 */
@Api(value = "/auth",description="系统认证",tags={"系统认证接口"})
@RestController
@RequestMapping("/api/auth/")
@Slf4j
@SuppressWarnings("all")
public class WechatAuthController {

    @Autowired
    private WechatAuthService wechatAuthService;

    @Autowired
    private WechatUserService wechatUserService;

    @ApiOperation("获取3rd_session")
    @PostMapping("get3rdSession")
    public ResultVO get3rdSession(@Valid WechatUserRequestDto wechatUserRequestDto, BindingResult bindingResult){
        log.info("WechatAuthController get3rdSession requestDto:{}",wechatUserRequestDto);
        //参数校验
        if(bindingResult.hasErrors()){
            return ResultVOUtil.error(bindingResult.getFieldError().getDefaultMessage());
        }
        WechatSignVO wechatSignVO = wechatAuthService.getWechatAuthSign(wechatUserRequestDto);
        if (null == wechatSignVO){
            return ResultVOUtil.error("微信获取信息错误");
        }
        return  ResultVOUtil.success(wechatSignVO);
    }
    /**
     * 是否认证身份.(是否绑定/手机号码/姓名)
     * @return
     */
    @ApiOperation("身份认证")
    @PostMapping("sign")
    public ResultVO sign(@RequestParam String openid){
        if (StringUtils.isEmpty(openid)){ return ResultVOUtil.error("参数不合法"); }
        WechatUser wechatUser = wechatUserService.getWechatUserByOpenid(openid);
        if (null == wechatUser){
            return ResultVOUtil.error(ResultVOUtil.FAIL,"会员尚未认证");
        }else {
            if (StringUtils.isEmpty(wechatUser.getMobile()) || StringUtils.isEmpty(wechatUser.getMobile())){
                return ResultVOUtil.error(ResultVOUtil.FAIL,"会员尚未认证");
            }
        }
        return  ResultVOUtil.success();
    }
}
